package pri.jz.keeper.server.aliyun.short_message

import android.util.Log
import com.aliyun.dysmsapi20170525.Client
import com.aliyun.dysmsapi20170525.models.SendSmsRequest
import com.aliyun.dysmsapi20170525.models.SendSmsResponse
import com.aliyun.tea.TeaException
import com.aliyun.teaopenapi.models.Config
import com.aliyun.teautil.Common
import com.aliyun.teautil.models.RuntimeOptions

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:用于阿里云的短信服务
 */
class DysmsAliyun {
    private val smsClient: Client by lazy { loadClient() }
    private val TAG = "DysmsFactory"
    private fun loadClient(): Client = Config().let {
        it.accessKeyId = AccessKeyId
        it.accessKeySecret = AccessKeySecret
        it.endpoint = "dysmsapi.aliyuncs.com"
        Client(it)
    }

    /**
     * 发送短信
     * @param smsRequest
     */
    fun sendSms(smsRequest: SendSmsRequest): SendSmsResponse {
        val runtime = RuntimeOptions()
        try {
            return smsClient.sendSmsWithOptions(smsRequest,runtime)
        } catch (error: TeaException) {
            Log.e(TAG, "TeaException: ${Common.assertAsString(error)}", )
        } catch (error: Exception) {
            Log.e(TAG, "Exception: ${TeaException(error.message,error).code}", )
        }
        return SendSmsResponse()
    }

    val AccessKeyId = "LTAI5tD4yWAhZDbDiDPSmprQ"
    val AccessKeySecret = "fbkKq7VD1n1YiEYux1w1TB9wqUJ8ew"


}